package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.wms.dto.StockDTO;
import com.itheima.wms.entity.StockEntity;
import com.itheima.wms.service.StockService;
import com.itheima.wms.vo.StockDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 库存
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/stock")
@Api(value = "Stock", tags = "库存")
public class StockController extends BaseController {

    @Autowired
    private StockService stockService;

    /**
     * 分页查询库存
     *
     * @param data 分页查询对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询库存", notes = "分页查询库存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "locationId", value = "库位id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "goodsTypeId", value = "货品类型id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "goodsId", value = "货品id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "total", value = "总库存", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "free", value = "可用库存", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "frozen", value = "冻结库存", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/page")
    public R<IPage<StockEntity>> page(@RequestParam Map data) {
        Page<StockEntity> page = getPage();
        IPage<StockEntity> stockPage = stockService.page(page, data);
        page.setRecords(stockPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "分页查询明细库存", notes = "分页查询明细库存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "locationName", value = "库位名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "locationCode", value = "库位编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerName", value = "货主名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "goodsName", value = "货品名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "owner", value = "货主名称/编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "goods", value = "货品名称/名称/条码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "locationId", value = "库位id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "needFree", value = "是否必须有可用库存 1 是", dataType = "Integer", paramType = "query"),

    })
    @GetMapping("/pageDetail")
    public R<IPage<StockDetailVO>> pageDetail(@RequestParam Map data) {
        Page<StockDetailVO> page = getPage();
        IPage<StockDetailVO> stockPage = stockService.pageDetail(page, data);
        page.setRecords(stockPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "查询全部库存", notes = "查询全部库存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "库区id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "locationId", value = "库位id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "ownerId", value = "货主id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "goodsTypeId", value = "货品类型id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "goodsId", value = "货品id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "total", value = "总库存", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "free", value = "可用库存", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "frozen", value = "冻结库存", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/list")
    public R<List<StockEntity>> list(@RequestParam Map data) {
        List<StockEntity> stockArray = stockService.list(data);
        return success(stockArray);
    }

    /**
     * 查询库存
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询库存", notes = "查询库存")
    @GetMapping("/{id}")
    public R<StockEntity> get(@PathVariable Long id) {
        return success(stockService.getById(id));
    }

    /**
     * 新增库存
     *
     * @param data 新增对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增库存", notes = "新增库存不为空的字段")
    @PostMapping
    public R<StockEntity> save(@RequestBody @Validated(SuperEntity.Save.class) StockDTO data) {
        stockService.save(data);
        return success(data);
    }

    /**
     * 修改库存
     *
     * @param data 修改对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改库存", notes = "修改库存不为空的字段")
    @PutMapping
    public R<StockEntity> update(@RequestBody @Validated(SuperEntity.Update.class) StockDTO data) {
        stockService.updateById(data);
        return success(data);
    }

    /**
     * 删除库存
     *
     * @param ids 主键id
     * @return 删除结果
     */
    @ApiOperation(value = "删除库存", notes = "根据id物理删除库存")
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        stockService.removeByIds(ids);
        return success();
    }

}
